<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>器械验收</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
</head>

<body>
<script type="text/javascript">

	var editIndex = undefined;
	//结束正在编辑的行
	function endEditing() {
		if (editIndex == undefined) {
			return true
		}
		if ($('#thpqxys').datagrid('validateRow', editIndex)) {
			$('#thpqxys').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	//事件：单击单元格的事件
	function onClickCell(index, field) {
		if (editIndex != index) {
			if (endEditing()) {
				$('#thpqxys').datagrid('selectRow', index).datagrid('beginEdit',
						index);
				var ed = $('#thpqxys').datagrid('getEditor', {
					index : index,
					field : field
				});
				if (ed) {
					($(ed.target).data('textbox') ? $(ed.target).textbox(
							'textbox') : $(ed.target)).focus();
				}
				editIndex = index;
			} else {
				setTimeout(function() {
					$('#thpqxys').datagrid('selectRow', editIndex);
				}, 0);
			}
		}
	}

	//结束编辑，同步数据
	function onEndEdit(index, row) {
		var ed = $(this).datagrid('getEditor', {
			index : index,
			field : 'cgUnit'
		});
		if(ed){
			row.cgUnit = $(ed.target).combobox('getText');
		}else{
			row.cgUnit = null;
		}
	}

	//添加行
	function append() {
		if (endEditing()) {
			$('#thpqxys').datagrid('appendRow', {
				'' : '',
				'' : '',
				'' : '',
				'' : '',
				'' : '',
				'' : '',
				'' : '',
				'' : ''
			});
			editIndex = $('#thpqxys').datagrid('getRows').length - 1;
			$('#thpqxys').datagrid('selectRow', editIndex);
			$('#thpqxys').datagrid('beginEdit',editIndex);
		}
	}

	//删除行
	function removeit() {
		if (editIndex == undefined) {
			return
		}
		$('#thpqxys').datagrid('cancelEdit', editIndex).datagrid('deleteRow',
				editIndex);
		editIndex = undefined;
	}

	//应用
	function accept() {
		if (endEditing()) {
			//$('#thpqxys').datagrid('acceptChanges');
			endEditing();
		}
	}

	//撤消
	function reject() {
		$('#thpqxys').datagrid('rejectChanges');
		editIndex = undefined;
	}

	//获取数据
	function getChanges() {
		var rows = $('#thpqxys').datagrid('getChanges');
		alert(rows.length + ' rows are changed!');
	}
	
	//合格数量判断
	function pdhgsl(n,o){
		//alert(n);
		var datas=$('#thpqxys').datagrid('getSelections');
		for(var i=0;i<datas.length;i++){
	        var data=datas[i];
	        //alert(datas[i].cgNumber);
	        var index=$('#thpqxys').datagrid('getRowIndex',data);
	        //var cgNumber=datas[i].cgNumber;
			//var o=parseInt(cgNumber)+parseInt(n)-parseInt(o);
			//$('#cgNumber').numberbox('setValue',o);
			//alert(index);
	        var number=$('#thpqxys').datagrid('getEditors',index);
	        var cgNumber=number[0].target.numberbox('getValue') *1;
	        var heNumber=number[2].target.numberspinner('getValue') *1;
	        // var bb=cgNumber+(o*1)-(n*1);
	      	// number[0].target.numberbox('setValue',bb);
	        // var r=bb+heNumber;
	        //alert(r);
	        if(n>cgNumber)
	        	number[2].target.numberspinner('setValue',cgNumber);
	    }
	}
	
$(function(){
	//提交采购计划
	$('#QiXieCheckBeforeAcceptance').linkbutton({
			onClick : function(){
				//$.messager.confirm("提示", "确定要提交验收吗？", function(v){
					//if(v){
						//将datagrid中的数据行保存到隐式表单域中
						//1、获取新增的数据行
						var rows = $('#thpqxys').datagrid('getRows');
						var today='${QiXieYSId }'*1;
						//为每一行生成对应的隐藏表单域
						var ThpSHRQ=$('#ThpSHRQ').datebox('getText');
						var ThpYSY=$('#ThpYSY').combogrid('getValue');
						var YsGysLXR=$('#YsGysLXR').textbox('getValue');
						var YsGysDH=$('#YsGysDH').textbox('getValue');
						var ff_checkAndAccept=$("#ff_checkAndAccept");
						for(var i = 0; i < rows.length; i ++){
							$("<input type='hidden' x='y'>").attr("name",
									"tHisPurchases[" + i + '].ycNo').val(
											today+i).appendTo(ff_checkAndAccept);
							$("<input type='hidden' x='y'>").attr("name",
									"tHisPurchases[" + i + '].ysTime').val(
											ThpSHRQ).appendTo(ff_checkAndAccept);
							/* $("<input type='hidden' x='y'>").attr("name",
									"tHisPurchases[" + i + '].gysTel').val(
											YsGysDH).appendTo(ff_checkAndAccept); */
							$("<input type='hidden' x='y'>").attr("name",
									"tHisPurchases[" + i + '].gysPeople').val(
											YsGysLXR).appendTo(ff_checkAndAccept);
							$("<input type='hidden' x='y'>").attr("name",
									"tHisPurchases[" + i + '].ygNo').val(
											ThpYSY).appendTo(ff_checkAndAccept);
							generateHidden(rows[i], "gysTel", i);
							generateHidden(rows[i], "cgNo", i);
							generateHidden(rows[i], "gysNo", i);
							generateHidden2(rows[i], "cgTotalprice", i);
						} 
						//提交表单
						if(rows.length != 0){
							$('#ff_checkAndAccept').form('submit', {
								success: function(v){
									$(":hidden[x='y']").remove();
									$('#thpqxys').datagrid('loadData', { total: 0, rows: [] });
									$.messager.alert('提示', '验收成功！');
									$('#ff_checkAndAccept').form("clear");
									$("#win-CheckAndAccept").window("close");
								}
							});
						}
					//}
			//});
		}
	});
	 
	function generateHidden(row, name, i) {
		var ff_checkAndAccept = $("#ff_checkAndAccept");
		$("<input type='hidden' x='y'>").attr("name",
				"tHisPurchases[" + i + '].'  + name).val(
				row[name]).appendTo(ff_checkAndAccept);
	}
	function generateHidden2(row, name, i) {
			var ff_checkAndAccept = $("#ff_checkAndAccept");
			$("<input type='hidden' x='y'>").attr("name",
					"tHisPurchases[" + i + '].'  + 'ysMenoy').val(
					row[name]).appendTo(ff_checkAndAccept);
	}
	 
	//根据计划ID查询所有采购信息
	$('#PurchasecgjhNo').combogrid({
		onClickCell: function (rowIndex, field,value) {
			//alert(value);
			//alert(typeof(value));
			$('#thpqxys').datagrid({
				url: 'queryPurchaseByPurchasePlanIdPage', 
				method: 'post', 
				queryParams: {
					cgjhNo: value
				}
			});
		}
	});
})
</script>
<style>
	*{
		font-size:12px;
	}
	#purchasePlan{
		width:100%;
		overflow: hidden;
	}
	 .thpqxysLI{
		float: left;
		list-style: none;
	}
</style>
	<!-- 器械验收 -->
	<!-- <div id="win-CheckAndAccept" class="easyui-window" title="器械验收" data-options="collapsible:false,
		minimizable:false,
		maximizable:false,
		modal:true,
		iconCls:'icon-project'"
		style="width: 806px; height: auto;"> -->
		<form id="ff_checkAndAccept" action="QiXieCheckandaccept" method="post">
			<div id="purchaseContent">
		        <div style="height:320px;border-bottom:1px solid #95B8e7;" class="easyui-layout" data-options="fit:true,border:false">
		        	<div style="background-color: rgb(236,243,255)" id="purchasePlan" data-options="region:'north',border:false">
						<ul>
							<li class="thpqxysLI" style="margin:0px 40px 10px 0px;">收货单号&nbsp;<input class="easyui-textbox" data-options="width: 150" readonly="readonly"
								value="${QiXieYSId }"></li>
							<li class="thpqxysLI" style="margin:0px 40px 10px 0px;">状态&nbsp;<select class="easyui-combobox" data-options="width: 80" readonly="readonly">
								<option value="未采购" selected="selected">未验收</option><option value="已采购">已验收</option></select></li>
							<li class="thpqxysLI" style="margin:0px 0px 10px 0px;">验收员&nbsp;<select id="ThpYSY" class="easyui-combogrid" data-options="width: 100,
								url:'findAllEmployee',
								idField:'ygNo',
								panelHeight:200,
								required: true,
								panelWidth:225,
								textField:'ygName',
								columns:[[
						            {field:'ygName',title:'姓名',width:50},
						            {field:'bmName',title:'部门',width:75},
						            {field:'ygPost',title:'职务',width:92}
								]]"></select></li>
							<li class="thpqxysLI" style="margin:0px 0px 10px 45px;">收货日期&nbsp;<input id="ThpSHRQ" class="easyui-datebox" data-options="required: true,width: 100"></li>
							<li class="thpqxysLI" style="margin:0px 0px 10px -24px;">采购计划单号&nbsp;<input id="PurchasecgjhNo" class="easyui-combogrid" data-options="width: 150,
								url:'selectAllHisPurchaseplan',
								idField:'cgjhNo',
								panelHeight:200,
								required: true,
								textField:'cgjhNo',
								columns:[[
						            {field:'cgjhNo',title:'采购计划ID',width:148}
								]]"></li>
							<li class="thpqxysLI" style="margin:0px 0px 10px 40px;">联系人&nbsp;<input id="YsGysLXR" class="easyui-textbox" data-options="required: true,width: 150"></li>
							<li class="thpqxysLI" style="margin:0px 0px 10px 116px;">电话&nbsp;<input id="YsGysDH" class="easyui-textbox" data-options="required: true,width: 150"></li>
						</ul>
					</div>
		        	
		        	<div data-options="region:'center',border:false">
		        		<table id="thpqxys" class="easyui-datagrid"
							data-options="
								fitColumns:true,
								fit:true,
								singleSelect: true,
								rownumbers: true,
								pagination: true,
								pageSize: 10,
								pageList: [5,10,15,20],
								toolbar: '#qxystb',
								border:false,
								onClickCell: onClickCell,
								onEndEdit: onEndEdit
								">
							<thead>
								<tr>
									<th data-options="field:'cgNo',width:'13%'">id</th>
									<th data-options="field:'cpName',width:'15%'">器械名称</th>
									<th data-options="field:'cgNumber',editor:{type:'numberbox',options:{readonly:true,min:0}},width:'10%'">采购数量</th>
									<th data-options="field:'cgUnit',width:'10%',editor:{
										type:'combobox',
										options:{
											readonly:true,
											valueField:'label',
											textField:'value',
											panelHeight: 'auto',
											data: [{label:'箱',value:'箱'},{label:'盒',value:'盒'},
											{label:'个',value:'个'},{label:'件',value:'件'},
											{label:'卷',value:'卷'},{label:'斤',value:'斤'},
											{label:'包',value:'包'}]}
										}">单位</th>
									<th data-options="field:'cgPrice',width:'7%'">单价</th>
									<th data-options="field:'cgTotalprice',width:'8%'">收货金额</th>
									<th data-options="field:'cgBatchnumber',width:'13%'">批号</th>
									<th data-options="field:'cgRemarks',width:'15%'">备注</th>
									<th data-options="field:'gysTel',width:'10%',editor:{type:'numberspinner',
										options:{min:0,value:0,onChange:pdhgsl}}">合格数量</th>
								</tr>
							</thead>
						</table>
						<div id="qxystb" style="height: auto;padding-left: 30px;">
							<a href="javascript:void(0)" class="easyui-linkbutton"
							data-options="iconCls:'icon-add',plain:true">添加一行</a>
							<a href="javascript:void(0)" class="easyui-linkbutton"
								data-options="iconCls:'icon-remove',plain:true"
								onclick="removeit()">移除一行</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true" onclick="accept()">应用</a>
							<a href="javascript:void(0)" class="easyui-linkbutton"
								data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
						</div>
		        	</div>
		        </div>
				<div style="margin:5px 5px 0px 0px;text-align: right;">
					<a id="QiXieCheckBeforeAcceptance" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">验收</a>
					<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:$('#win-CheckAndAccept').window('close');" onclick="">取消</a>
				</div>
			</div>
		</form>
	<!-- </div> -->
</body>
</html>